<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage library
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: library/socket.php 1.1 2011-02-10 14:39 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

class Library_Socket
{
	public $host;			//通信地址
    public $port;			//通信端口
    public $limit_time = 0;	//连接超时时间
    public $backlog = 3;	//请求队列中允许的最大请求数
	public $socket = null;	//Socket句柄
	
	private $result = null;
	private $spawn = null;
	private $input = null;
	
	//----------------------------------------------------------
	/**
	 * 构造函数
	 * 
	 * @return void
	 */
	public function __construct()
	{
		set_time_limit($this->limit_time);
	}
	
	//----------------------------------------------------------
	/**
	 * 创建一个Socket
	 *
	 * @return void
	 */
	public function create()
	{
		try {
			$this->socket = socket_create(AF_INET, SOCK_STREAM, 0) or false;
			if (!$this->socket) {
				throw new Exception('Could not create socket');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 绑定Socket到端口,监听外部连接
	 *
	 * @return void
	 */
	public function bind_listen()
	{
		$this->bind();
		$this->listen();
	}

	//----------------------------------------------------------
	/**
	 * 绑定Socket到端口
	 *
	 * @return void
	 */
	public function bind()
	{
		try {
			$this->result = socket_bind($this->socket, $this->host, intval($this->port)) or false;
			if (!$this->socket) {
				throw new Exception('Could not bind to socket');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 开始监听链接
	 *
	 * @param int $backlog 请求队列中允许的最大请求数
	 * @return void
	 */
	public function listen()
	{
		try {
			$this->result = socket_listen($this->socket, intval($this->backlog)) or false;
			if (!$this->socket) {
				throw new Exception('Could not set up socket listener');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * Socket通信处理
	 *
	 * @return void
	 */
	public function accept()
	{
		try {
			$this->spawn = socket_accept($this->socket) or false;
			if (!$this->spawn) {
				throw new Exception('Could not accept incoming connection');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 获取客户端输入
	 *
	 * @param int $length
	 * @param int $type
	 * @return void
	 */
	public function read($length = 1024, $type = null)
	{
		try {
			$this->input = socket_read($this->spawn, intval($length), $type) or false;
			if (!$this->input) {
				throw new Exception('Could not read input');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 处理客户端输入并返回结果
	 *
	 * @return void
	 */
	public function write()
	{
		try {
			$input = trim($this->input); // 清空输入字符串
			$output = strrev($input) . "\n"; //处理客户端输入并返回结果
			if (socket_write($this->spawn, $output, strlen ($output))) {
				throw new Exception('Could not write output');
			}
		}catch (Exception $e){
			echo $e->message();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 关闭Socket
	 *
	 * @return void
	 */
	public function close()
	{
		socket_close($this->spawn);
		socket_close($this->socket);
	}
	
	//----------------------------------------------------------
	/**
	 * 析构函数
	 *
	 * @return void
	 */
	public function __destruct()
	{
		$this->close();
	}
}